package com.glory.dgcpcpda.activity.cpc;

import android.content.Intent;
import android.os.Environment;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.PrintEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;

public class SealConfirmLetterActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;

    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;

    @BindView(R.id.llMain)
    LinearLayout llMain;
    // 属性参数
    private String url;
    private Boolean isDelete;

    public void getData(String lotId){
        FreeUI_EntityCallBack<BaseResponseBean<PrintEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PrintEntity>>
                (new TypeToken<BaseResponseBean<PrintEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PrintEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    PrintEntity body = response.body().getResponse().getBody();
                    String code = body.getCode();
                    String[] s = code.split(" ");
                    byte[] bytes = new byte[s.length];

                    for (int i = 0; i < s.length; i++){
                        int i1 = Integer.parseInt(s[i]);
                        byte bys = (byte) i1;
                        bytes[i] = bys;
                    }

                    File file=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "sealcon.pdf");
                    String absolutePath = file.getAbsolutePath();

                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream(absolutePath);
                        fileOutputStream.write(bytes, 0, bytes.length);
                        fileOutputStream.close();
                        //创建意图
                        Intent intent = new Intent(SealConfirmLetterActivity.this,PdfViewActivity.class);
                        //传值
                        intent.putExtra("url",absolutePath);
                        intent.putExtra("isDelete",true);
                        //执行意图
                        startActivity(intent);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PrintEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "SealConfirmLetter");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOTID",lotId);
        NetUtils.requestPostNet("CPC.LOTQUERY", "SealConfirmLetter", this, map, callBack);
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_sealconfirmletter;
    }

    @Override
    protected void initView() {
        tvTitle.setText("印章确认书");
        setMainLayout(llMain);
        tvBaseSubmit.setVisibility(View.GONE);
        initEdit(etLot,s ->getData(etLot.getText().toString().trim()) );
    }

    @Override
    protected void initData() {
    }

    protected void initEdit2(EditText editText, final editTextListener listener){
        editText.setImeOptions(EditorInfo.IME_ACTION_GO);
        editText.setSingleLine(true);
        editText.setOnEditorActionListener((textView, i, keyEvent) -> {
            if (EditorInfo.IME_ACTION_GO == i) {
                String text = editText.getText().toString();
                editText.getText().clear();
                editText.setText(text);
                editText.setSelection(editText.getText().length());
                return true;
            }
            return false;
        });
        FreeApi_EditTextUtils.setEditTextListener(editText, this, s -> {
            if (listener != null){
                listener.onTextChangeListener(s);
            }
        });
    }

}


